<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
    <title>芳小微宣</title>
    <script src="js/jquery.min.js"> </script>
    <script src="js/jquery.cookie.js"> </script>
    <script src="js/index.js"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    <!-- 引入 WeUI -->
    <link rel="stylesheet" href="http://res.wx.qq.com/open/libs/weui/0.4.0/weui.min.css">
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="js/uploader/uploader.css">
    <script src="js/uploader/Q.js"></script>
    <script src="js/uploader/Q.Uploader.js"></script>
    <script src="js/uploader/Q.Uploader.UI.js"></script>
    <script src="js/Eleditor.js"></script>
    <style>
        .titSty{
            position: relative;
            top: -10px;
            font-size: 4rem;
            left: 20px;
            width: 170px;
            height: 30px;
            border-radius: 5px;
            border: 1px solid #2996a9;
            text-indent: 5px;
        }
        .contentin {
            padding-top: 0px;
            margin-left: 0px;
        }
        .tabChange{
            width: 50%;
            height: 40px;
            line-height: 40px;
            float: left;
            text-align: center;
            background:#e5e5e5;
            font-size: 4.5rem;
            color: #848587;
        }
        .gonggao-tit{
            font-size: 4.8rem;
            color: #adadad;
            margin-left: 20px;
        }
        .gonggao-text{
            width: 90%;
           /* height: 30px;*/
            margin: 10px 5%;
            font-size: 4.5rem;
        }
        .gonggao-text .gonggao-word{
            width: 100%;
            height: 30px;
            border-radius: 5px;
            border: 1px solid #adadad;
            text-indent: 5px;
        }
        #meditToolBar {
            position:fixed;
            z-index:100;
            width:90%;
            left: 5%;
            top: 0;
            height:40px;
        }
        #medit {
            padding: 10px;
            border:1px solid #ccc;
            height:360px;
            /*margin-top:48px;*/
            overflow: auto;
        }
        #contentEditor{
            width: 100%;
            min-height: 300px;
            box-sizing: border-box;
            padding: 10px;
            color: #444;
        }
        #contentEditor p{
            letter-spacing: 0.25px;
            font: 16px/25px Tahoma, Verdana, 宋体;
            margin: 20px 0px;
        }
        #contentEditor h4 {
            font-weight: bold;
            line-height: 1.333em;
            margin: 10px 0 20px;
            padding: 25px 0 0;
        }
        #contentEditor img{
            width: 100%;
            height: auto;
            box-sizing: border-box;
        }
        .dempTip{
            border-left: 2px solid #00BCD4;
            padding-left: 5px;
            margin: 10px;
            font-size: 16px;
        }
        code{
            white-space: pre-wrap;
            background: #2D2D2D;
            display: block;
            margin: 10px;
            border-radius: 5px;
            color: #fff;
        }
        .viewTit{
            color: #FF5722;
            position: fixed;
            top: 0;
            left: 0;
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            text-align: center;
            display: block;
            width: 100%;
            background: #FFEB3B;
            box-shadow: 0 0 5px;
        }
        .content{
            float: none;
        }
        .fl{
            width: 65%;
        }
        .u-name{
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            width: 100%;
        }
    </style>
</head>
<body>
     <div class="weui_cells_title">信息发布</div>
     <div class="tabChange" style="background: #00a1e9;color: #fff">通知</div>
     <div class="tabChange" >公告</div>

     <div style="clear: both"></div>
     <div class="gonggao" style="margin-bottom: 60px;">
         <div style="margin-top: 6px;"><span class="gonggao-tit">通知主题</span><div class="gonggao-text"><input type="text" class="gonggao-word tzzt" /></div> </div>
         <div><span class="gonggao-tit">通知内容</span>
             <div class="gonggao-text" style="border: 1px solid #adadad;">
                 <div id="contentEditor"></div>
             </div>
         </div>
         <div><span class="gonggao-tit">发布部门</span><div class="gonggao-text"> <input type="text" class="gonggao-word fbbm" /></div> </div>
         <div><span class="gonggao-tit">推送选择</span>
             <div class="gonggao-text">
                 <input type="checkbox" value="" id="selectAll"/>全选
                 <input name="pushChoose" type="checkbox" value="3" />办公室
                 <input name="pushChoose" type="checkbox" value="1" />校牌
                 <input name="pushChoose" type="checkbox" value="0" />班牌
             </div>
         </div>
     </div>
    <div class="main fileUpload"  style="display: none; margin-bottom: 60px;">
       <!-- <span style=" position: relative;top: -15px; font-size: 4.8rem;  left: 10px;">设置标题</span>
        <input type="text" placeholder="输入标题" name="title" id="titleName"  class="titSty" style="top: -15px"/>-->
        <div class="upl-int">输入标题</div>
        <textarea class="upl-textarea"></textarea>
        <div class="upl-subline"></div>
        <div class="upl-addfile">添加附件</div>
        <div class="content">
            <div class="contentin">
                <div>
                    <a id="upload-targetFk" class="x-button" style="_background:#2783ce;_color:white;_text-align:center;_display:inline-block;_height:40px;_line-height:40px;_width:130px;">点击上传附件</a>
                </div>
            </div>
        </div>
        <div id="upload-viewFk"></div>

    </div>

     <input type="button" class="photo_subCon" value="提交" onclick="subInfo()"/>

    <div class="weui_dialog_alert" style="display: none;">
        <div class="weui_mask"></div>
        <div class="weui_dialog">
            <div class="weui_dialog_hd"> <strong class="weui_dialog_title">警告</strong>
            </div>
            <div class="weui_dialog_bd">弹窗内容，告知当前页面信息等</div>
            <div class="weui_dialog_ft">
                <a href="javascript:;" class="weui_btn_dialog primary">确定</a>
            </div>
        </div>
    </div>

<script>

    $.weui = {};
    $.weui.alert = function(options ,tag , hrefWhat){
        options = $.extend({title: '提示', text: '提示内容'}, options);
        var $alert = $('.weui_dialog_alert');
        $alert.find('.weui_dialog_title').text(options.title);
        $alert.find('.weui_dialog_bd').text(options.text);
        $alert.on('touchend click', '.weui_btn_dialog', function(){
            if(tag == "succ"){
                if(hrefWhat && hrefWhat == "gg"){
                    window.location.href = "tzggList.html?pageIndex=2";
                }else{
                    window.location.href = "tzggList.html";
                }
            }else if(tag == "err"){
                $alert.hide();
            }
            //阻止事件冒泡
            return false;
        });
        $alert.show();
    };
    $(".tabChange").click(function(){
        $(".tabChange").css("background","#e5e5e5");
        $(".tabChange").css("color","#848587");
        $(this).css("background","#00a1e9");
        $(this).css("color","#fff");
        if($(this).index() == 1){
            $(".main ").css("display","none");
            $(".gonggao ").css("display","block");
        }else{
            $(".main ").css("display","block");
            $(".gonggao ").css("display","none");
        }
    });

    var filesid = new Array();
    var removeFilesid = new Array();
    var uploadGroup =[];
    var contentEditor;
    $(function(){
        uploadFj("upload-targetFk" , "upload-viewFk" ,filesid, removeFilesid );
         contentEditor = new Eleditor({
            el: '#contentEditor',
            upload:{
                server: path + "/api/weixin/schoolImg?token=" + window.localStorage.getItem('token'),
                compress: false,
                fileSizeLimit: 100
            },
            toolbars: [
                'insertText',
                'editText',
                //'insertImage',
                'insertLink',
                'deleteBefore',
                'deleteAfter',
                'insertHr',
                'deleteThis',
                'cancel'
            ]
            //placeHolder: 'placeHolder设置占位符'
        });
    });
    $("#selectAll").click(function(){
        if (this.checked) {
            $(".gonggao-text :checkbox").attr("checked", true);
        } else {
            $(".gonggao-text :checkbox").attr("checked", false);
        }
    });

    //附件上传
    function uploadFj(targetEle , viewEle ,filesid , removeFilesid){
        var Uploader = Q.Uploader;
        var uploader = new Uploader({
            url: path + "/api/weixin/file?token=" + window.localStorage.getItem('token'),
            target: document.getElementById(targetEle),
            view: document.getElementById(viewEle),
            upName: "file",
            multiple: false,
            //每次上传都会发送的参数(POST方式)
            data: { user: "Devin"},
            contentType : false,
            on: {
                //任务移除后触发
                remove: function (task) {
                    var json = task.jsonData;
                    removeFilesid.push({"attUrl":json.attUrl,"name":json.name});
                },
                //上传之前触发
                upload: function (task) {
                    var extension = task.ext;
                    if (!(extension && /^(.doc|.docx|.pdf)$/.test(extension.toLowerCase()))){
                        $.weui.alert({text: "只支持word和 pdf格式的图片！"},"err");
                        $("#" + viewEle).find($(".u-item")).eq($("#" + viewEle).find($(".u-item")).length-1).remove();
                        return false;
                    }
                    $(".photo_subCon").attr("disabled", true);
                },
                //上传完成后触发
                complete: function (task) {
                    $(".photo_subCon").attr("disabled", false);
                    var jsonFj = task.jsonData;
                    if(jsonFj){
                        if (this.index >= this.list.length - 1) {
                            //所有任务上传完成
                            filesid.push({"attUrl":jsonFj.attUrl,"name":jsonFj.name});
                        }
                    }
                }
            }
        });
        uploadGroup.push(uploader);
    };


    function subInfo(){
        //  公告页
        if($(".main").css("display") == "block"){
            if($(".upl-textarea").val().replace(/[ ]/g, "") == ""){
                $.weui.alert({text: "请输入标题！"},"err");
            }else if(filesid.length <= removeFilesid.length){
                $.weui.alert({text: "请上传附件！"},"err");
            }else{
                $.ajax({
                    type : "POST",
                    url : path + "/api/weixin/saveNotice",
                    data : {
                        title : $(".upl-textarea").val(),
                        fileList : JSON.stringify(filesid),
                        delList : JSON.stringify(removeFilesid),
                        token : window.localStorage.getItem("token")
                    },
                    dataType : 'json',
                    success : function(msg) {
                        if(msg.code == 0){
                            $.weui.alert({text: "提交成功！"},"succ" ,"gg");
                        }else if(msg.code == 501){
                            window.location.href = "login.html";
                        }
                    }
                });
            }
        }else{
            if($(".tzzt").val().replace(/[ ]/g, "") == ""){
                $.weui.alert({text: "请输入通知主题！"},"err");
            }else if(contentEditor.getContent().replace(/[ ]/g, "") == ""){
                $.weui.alert({text: "请输入通知内容！"},"err");
            }else if($(".fbbm").val().replace(/[ ]/g, "") == ""){
                $.weui.alert({text: "请输入发布部门！"},"err");
            }else if(!$("input[name='pushChoose']").is(":checked")){
                $.weui.alert({text: "请选择推送！"},"err");
            }else{
                var departData = [];
                $("input:checkbox[name='pushChoose']:checked").each(function() { // 遍历name=test的多选框
                    departData.push($(this).val());  // 每一个被选中项的值
                });
                $.ajax({
                    type : "POST",
                    url : path + "/api/weixin/addNotice",
                    data : {
                        theme : $(".tzzt").val(),
                        content : contentEditor.getContent(),
                        department : $(".fbbm").val(),
                        ids : JSON.stringify(departData),
                        token : window.localStorage.getItem("token")
                    },
                    dataType : 'json',
                    success : function(msg) {
                        if(msg.code == 0){
                             $.weui.alert({text: "提交成功！"},"succ");
                        }else if(msg.code == 501){
                            window.location.href = "login.html";
                        }
                    }
                });
            }
        }
    }
</script>
</body>
</html>